﻿#Region "Imports"
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
#End Region

#Region "ColorPickerHelper Class"
''' <summary>
''' Color Picker Helper Class
''' </summary>
''' <remarks>Version 1.0.0</remarks>
Public Class ColorPickerHelper

#Region "   Private Variables"

    Private _Parent As Control
    Private _Button As ColorPickerButton
    Private _IsFreez As Boolean

#End Region

#Region "   Constructor"
    ''' <summary>
    ''' Create New Instance Of ColorPicker.
    ''' </summary>
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
    End Sub
    ''' <summary>
    ''' Create New Instance Of Color Picker.
    ''' </summary>
    Public Sub New(ByVal Button As ColorPickerButton)
        InitializeComponent()
        _Button = Button
    End Sub

#End Region

#Region "   Properties"

    Private _SelectedColor As Color = Color.Black
    ''' <summary>
    ''' Get or Set the Selected Color For ColorPicker.
    ''' </summary>
    Public Property SelectedColor() As Color
        Get
            Return _SelectedColor
        End Get
        Set(ByVal value As Color)
            _SelectedColor = value
        End Set
    End Property

#End Region

#Region "   Methods"
    ''' <summary>
    ''' Show ColorPicker On Click Of ColorPickerButton
    ''' </summary>
    ''' <param name="ParentControl">ParentControl As Control</param>
    ''' <param name="_Location">Location As Point</param>
    Public Overloads Sub Show(ByVal ParentControl As Control, ByVal _Location As Point)
        _Parent = ParentControl
        Me.Location = ParentControl.PointToScreen(_Location)
        Me.Show()
    End Sub
    ''' <summary>
    ''' Handles When ColorPicker Deactivate.
    ''' </summary>
    ''' <param name="e">e As System.EventArgs</param>
    Protected Overrides Sub OnDeactivate(ByVal e As System.EventArgs)
        If Not _IsFreez Then
            Me.Hide()
        End If
    End Sub
    ''' <summary>
    ''' Handles Mouse Leave For ColorPicker.
    ''' </summary>
    ''' <param name="e">ByVal e As System.EventArgs</param>
    Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
        MyBase.OnLeave(e)
        Me.Hide()
    End Sub

    Private Sub MBColorPicker_SelectedColorChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ColorPicker.SelectedColorChanged
        Me.SelectedColor = Me.ColorPicker.SelectedColor
        _Button.PickedColor = Me.SelectedColor
        _Button.Refresh()
        OnDeactivate(e)
    End Sub

    Private Sub MBColorPicker_ColorPalleteSelected(ByVal sender As Object, ByVal e As EventArgs) Handles ColorPicker.ColorPaletteSelected
        _IsFreez = True
        Dim color As Color = Drawing.Color.White
        Dim _ColorDialogBox As ColorDialog = New ColorDialog
        If _ColorDialogBox.ShowDialog = DialogResult.OK Then
            Me.SelectedColor = _ColorDialogBox.Color
            _Button.PickedColor = Me.SelectedColor
            _Button.Refresh()
            _IsFreez = False
        End If
        OnDeactivate(e)
    End Sub

#End Region

#Region "   ColorPicker"
    ''' <summary>
    ''' Color Picker Class
    ''' </summary>
    ''' <remarks>Version 1.0.0</remarks>
    Friend Class iColorPicker
        Inherits System.Windows.Forms.UserControl

#Region "   Private Variables"

        Private _ColorBoxSize As Size = New Size(12, 12)
        Private Const _ColorBoxUnit As Int16 = 12
        Private Const _ColorBoxMarginX As Int16 = 3
        Private Const _ColorMatrixX As Int16 = 10
        Private Const _ColorMatrixY As Int16 = 7
        Private Const _TopBoxMargin1 As Int16 = _ColorBoxUnit + 4
        Private Const _TopBoxMargin2 As Int16 = _TopBoxMargin1 + _ColorBoxUnit + 4
        Private Const _TopBoxMargin3 As Int16 = _TopBoxMargin2 + 6 * _ColorBoxUnit + 8
        Private _Font As Font = New Font("Tahoma", 8, FontStyle.Bold)
        Private _SelectedBox As Point = New Point(-1, -1)
        Private _IsPaletteSelectedG As Boolean = False
        Private _ColorMatrix As Color(,)
        Private _SelectedColor As Color = Color.Black

#End Region

#Region "   Constructor"
        ''' <summary>
        ''' Create New Instance Of ColorPicker.
        ''' </summary>
        Public Sub New()
            SetStyle(ControlStyles.DoubleBuffer, True)
            SetStyle(ControlStyles.AllPaintingInWmPaint, True)
            SetStyle(ControlStyles.ResizeRedraw, True)
            SetStyle(ControlStyles.SupportsTransparentBackColor, True)
            InitializeComponent()
            CreateColorMatrix()
        End Sub
        ''' <summary>
        ''' Initialize all components for ColorPicker.
        ''' </summary>
        Private Sub InitializeComponent()
            Me.SuspendLayout()
            '
            'ColorPicker
            '
            Me.BackColor = System.Drawing.Color.FromArgb(CType(CType(216, Byte), Integer), CType(CType(228, Byte), Integer), CType(CType(248, Byte), Integer))
            Me.Name = "ColorPicker"
            Me.Size = New System.Drawing.Size(155, 145)
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region "   Events"

        Public Event SelectedColorChanged As EventHandler
        Public Event ColorPaletteSelected As EventHandler

#Region "Mouse Events"
        ''' <summary>
        ''' Handles Mouse Move For ColorPicker.
        ''' </summary>
        ''' <param name="e">e As System.Windows.Forms.MouseEventArgs</param>
        Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
            MyBase.OnMouseMove(e)
            _SelectedBox = GetSelectedBox(e.Location)
            Invalidate()
        End Sub
        ''' <summary>
        ''' Handles Mouse Leave For ColorPicker.
        ''' </summary>
        ''' <param name="e">e As System.EventArgs</param>
        Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
            MyBase.OnMouseLeave(e)
            _IsPaletteSelectedG = False
            Me.Invalidate()
        End Sub
        ''' <summary>
        ''' Handles Mouse Click For ColorPicker.
        ''' </summary>
        ''' <param name="e">e As System.Windows.Forms.MouseEventArgs</param>
        Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.MouseEventArgs)
            MyBase.OnMouseClick(e)
            If (_IsPaletteSelectedG) Then
                RaiseEvent ColorPaletteSelected(Me, e)
            End If
            If (_SelectedBox.X > -1 And _SelectedBox.Y > -1) Then
                _SelectedColor = _ColorMatrix(_SelectedBox.X, _SelectedBox.Y)
                RaiseEvent SelectedColorChanged(Me, e)
            End If
        End Sub

#End Region

#End Region

#Region "   Properties"
        ''' <summary>
        ''' Get or Set Selected Color For ColorPicker.
        ''' </summary>
        Public Property SelectedColor() As Color
            Get
                Return _SelectedColor
            End Get
            Set(ByVal value As Color)
                _SelectedColor = value
                Me.Invalidate()
            End Set
        End Property

#End Region

#Region "   Methods"
        ''' <summary>
        ''' Create Color Matrix For ColorPicker.
        ''' </summary>
        Public Sub CreateColorMatrix()
            _ColorMatrix = New Color(,) {{GetColor(255, 255, 255), GetColor(210, 210, 210), GetColor(186, 186, 186), GetColor(154, 154, 154), _
          GetColor(130, 130, 130), GetColor(114, 114, 114), GetColor(178, 14, 18)}, _
         {GetColor(0, 0, 0), GetColor(114, 114, 114), GetColor(106, 106, 106), GetColor(78, 78, 78), _
          GetColor(54, 54, 54), GetColor(30, 30, 30), GetColor(234, 22, 30)}, _
         {GetColor(246, 234, 210), GetColor(182, 174, 166), GetColor(154, 150, 142), GetColor(114, 110, 106), _
          GetColor(78, 74, 70), GetColor(54, 50, 50), GetColor(254, 186, 10)}, _
         {GetColor(26, 62, 114), GetColor(194, 206, 218), GetColor(134, 154, 182), GetColor(78, 106, 150), _
          GetColor(18, 46, 82), GetColor(10, 30, 54), GetColor(255, 255, 0)}, _
         {GetColor(82, 122, 174), GetColor(210, 222, 243), GetColor(166, 186, 214), GetColor(122, 154, 194), _
          GetColor(58, 86, 126), GetColor(38, 54, 78), GetColor(150, 214, 66)}, _
         {GetColor(186, 70, 66), GetColor(238, 206, 206), GetColor(218, 158, 158), GetColor(202, 114, 110), _
          GetColor(134, 50, 46), GetColor(86, 34, 30), GetColor(26, 170, 66)}, _
         {GetColor(150, 182, 86), GetColor(226, 238, 210), GetColor(202, 218, 170), GetColor(174, 198, 126), _
          GetColor(106, 130, 62), GetColor(66, 82, 38), GetColor(2, 178, 238)}, _
         {GetColor(128, 102, 160), GetColor(223, 216, 231), GetColor(191, 178, 207), GetColor(159, 140, 183), _
          GetColor(96, 76, 120), GetColor(64, 51, 80), GetColor(0, 114, 188)}, _
         {GetColor(75, 172, 198), GetColor(210, 234, 240), GetColor(165, 213, 226), GetColor(120, 192, 212), _
          GetColor(56, 129, 148), GetColor(37, 86, 99), GetColor(47, 54, 153)}, _
         {GetColor(245, 157, 86), GetColor(252, 230, 212), GetColor(250, 206, 170), GetColor(247, 181, 128), _
          GetColor(183, 117, 64), GetColor(122, 78, 43), GetColor(111, 49, 152)}}
        End Sub
        ''' <summary>
        ''' Handles SelectedColor Change Event.
        ''' </summary>
        Private Sub OnSelectedColorChanged()
            RaiseEvent SelectedColorChanged(Me, New EventArgs())
        End Sub
        ''' <summary>
        ''' Handles Paint For ColorPicker.
        ''' </summary>
        ''' <param name="e">System.Windows.Forms.PaintEventArgs</param>
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            DrawColorBoxes(e.Graphics)
            DrawTexts(e.Graphics, _Font)
            DrawBorder(e.Graphics)
            DrawSelectedBox(e.Graphics)
            DrawPalette(e.Graphics)
        End Sub

#Region "   Drawing Methods"
        ''' <summary>
        ''' Draw Background For ColorPicker.
        ''' </summary>
        ''' <param name="graphics">graphics As Graphics</param>
        Private Sub DrawBackground(ByVal graphics As Graphics)
            graphics.FillRectangle(New SolidBrush(Color.FromArgb(250, 250, 250)), Me.ClientRectangle)
            graphics.FillRectangle(New SolidBrush(Color.FromArgb(221, 231, 238)), New Rectangle(2, 2, Me.Width - 4, _ColorBoxUnit))
            graphics.FillRectangle(New SolidBrush(Color.FromArgb(221, 231, 238)), New Rectangle(2, _TopBoxMargin2 + 5 * _ColorBoxUnit + 5, Me.Width - 4, _ColorBoxUnit))
        End Sub
        ''' <summary>
        ''' Draw Border For ColorPicker.
        ''' </summary>
        ''' <param name="graphics">Graphics As Graphics</param>
        Private Sub DrawBorder(ByVal graphics As Graphics)
            graphics.DrawRectangle(New Pen(Color.LightGray), 0, 0, (_ColorBoxUnit + _ColorBoxMarginX) * _ColorMatrixX + _ColorBoxMarginX + 1, _TopBoxMargin3 + 32)
        End Sub
        ''' <summary>
        ''' Draw Text For ColorPicker.
        ''' </summary>
        ''' <param name="graphics">Graphics As Graphics</param>
        ''' <param name="font">Font As Font</param>
        Private Sub DrawTexts(ByVal graphics As Graphics, ByVal font As Font)
            graphics.DrawString("Theme colors", font, New SolidBrush(Color.Navy), 1, 1)
            graphics.DrawString("Standard colors", font, New SolidBrush(Color.Navy), 1, _TopBoxMargin2 + 5 * _ColorBoxUnit + 4)
        End Sub
        ''' <summary>
        ''' Draw Color Boxes For ColorPicker.
        ''' </summary>
        ''' <param name="graphics">Graphics As Graphics</param>
        Private Sub DrawColorBoxes(ByVal graphics As Graphics)
            DrawColorBelt(graphics, 0, _TopBoxMargin1, True)
            For i As Int16 = 1 To _ColorMatrixY - 2
                DrawColorBelt(graphics, i, (i - 1) * _ColorBoxUnit + _TopBoxMargin2, False)
            Next
            For i As Int16 = 0 To _ColorMatrixX - 1
                graphics.DrawRectangle(New Pen(Color.Gray), i * (_ColorBoxUnit + _ColorBoxMarginX) + _ColorBoxMarginX, _TopBoxMargin2, _ColorBoxUnit, _ColorBoxUnit * 5)
            Next
            DrawColorBelt(graphics, _ColorMatrixY - 1, _TopBoxMargin3, True)
        End Sub
        ''' <summary>
        ''' Draw Color Belt For ColorPicker.
        ''' </summary>
        ''' <param name="graphics">Graphics As Graphics</param>
        ''' <param name="no">No As Int16</param>
        ''' <param name="yOffSet">yOffSet As Int16</param>
        ''' <param name="border">Border As Boolean</param>
        ''' <remarks></remarks>
        Private Sub DrawColorBelt(ByVal graphics As Graphics, ByVal no As Int16, ByVal yOffSet As Int16, ByVal border As Boolean)
            For i As Int16 = 0 To _ColorMatrixX - 1
                DrawColorBox(graphics, _ColorMatrix(i, no), New Point(i * (_ColorBoxUnit + _ColorBoxMarginX) + _ColorBoxMarginX, yOffSet), border)
            Next
        End Sub
        ''' <summary>
        ''' Draw Color Box For ColorPicker.
        ''' </summary>
        ''' <param name="graphics">Graphics As Graphics</param>
        ''' <param name="color">Color As Color</param>
        ''' <param name="location">Location As Point</param>
        ''' <param name="border">Border As Boolean</param>
        Private Sub DrawColorBox(ByVal graphics As Graphics, ByVal color As Color, ByVal location As Point, ByVal border As Boolean)
            graphics.FillRectangle(New SolidBrush(color), New Rectangle(location, _ColorBoxSize))
            If (border) Then
                graphics.DrawRectangle(New Pen(Drawing.Color.LightGray), New Rectangle(location, _ColorBoxSize))
            End If
        End Sub
        ''' <summary>
        ''' Draw Selected Box For ColorPicker.
        ''' </summary>
        ''' <param name="graphics">graphics As Graphics</param>
        Private Sub DrawSelectedBox(ByVal graphics As Graphics)
            If (_SelectedBox.X > -1 And _SelectedBox.Y > -1) Then
                Dim point As Point = New Point(0, 0)

                point.X = _SelectedBox.X * (_ColorBoxUnit + 3) + 3

                If (_SelectedBox.Y = 0) Then
                    point.Y = _TopBoxMargin1
                ElseIf (_SelectedBox.Y = 6) Then
                    point.Y = _TopBoxMargin3
                Else
                    point.Y = _TopBoxMargin2 + (_SelectedBox.Y - 1) * _ColorBoxUnit
                End If
                Dim _Rect As Rectangle = New Rectangle(point.X - 1, point.Y - 1, _ColorBoxUnit + 2, _ColorBoxUnit + 2)
                DrawGradientBorder(graphics, _Rect, _itemToolItemSelectedColors)
                graphics.DrawRectangle(New Pen(Color.White), New Rectangle(point, _ColorBoxSize))
                graphics.DrawRectangle(New Pen(Color.OrangeRed), point.X - 1, point.Y - 1, _ColorBoxUnit + 2, _ColorBoxUnit + 2)
            End If
        End Sub
        ''' <summary>
        ''' Draw Color Palette For ColorPicker.
        ''' </summary>
        ''' <param name="graphics">graphics As Graphics</param>
        Private Sub DrawPalette(ByVal graphics As Graphics)
            graphics.DrawImage(My.Resources.ColorPalette, 2, _TopBoxMargin3 + _ColorBoxUnit + 2)
            graphics.DrawString("More colors...", _Font, New SolidBrush(Color.Navy), 20, _TopBoxMargin3 + _ColorBoxUnit + 3)
            If (_IsPaletteSelectedG) Then
                Dim _Rect As Rectangle = New Rectangle(0, _TopBoxMargin3 + _ColorBoxUnit + 1, Me.Width - 5, _ColorBoxUnit + 6)
                DrawGradientBack(graphics, _Rect, _itemToolItemSelectedColors)
                DrawGradientBorder(graphics, _Rect, _itemContextItemEnabledColors)
                graphics.DrawImage(My.Resources.ColorPalette, 2, _TopBoxMargin3 + _ColorBoxUnit + 2)
                graphics.DrawString("More colors..", _Font, New SolidBrush(Color.Navy), 20, _TopBoxMargin3 + _ColorBoxUnit + 3)
            End If
        End Sub
        ''' <summary>
        ''' Draw Gradient Background for MBToolStrip/MBStatusStrip
        ''' </summary>
        Private Sub DrawGradientBack(ByVal g As Graphics, ByVal backRect As Rectangle, ByVal colors As GradientItemColor)
            backRect.Inflate(-1, -1)
            Dim y2 As Int32 = backRect.Height / 2
            Dim backRect1 As Rectangle = New Rectangle(backRect.X, backRect.Y, backRect.Width, y2)
            Dim backRect2 As Rectangle = New Rectangle(backRect.X, backRect.Y + y2, backRect.Width, backRect.Height - y2)
            Dim backRect1I As Rectangle = backRect1
            Dim backRect2I As Rectangle = backRect2
            backRect1I.Inflate(1, 1)
            backRect2I.Inflate(1, 1)
            Using insideBrush1 As LinearGradientBrush = New LinearGradientBrush(backRect1I, colors.InsideTop1, colors.InsideTop2, 90.0F), _
                insideBrush2 = New LinearGradientBrush(backRect2I, colors.InsideBottom1, colors.InsideBottom2, 90.0F)
                g.FillRectangle(insideBrush1, backRect1)
                g.FillRectangle(insideBrush2, backRect2)
            End Using
            y2 = backRect.Height / 2
            backRect1 = New Rectangle(backRect.X, backRect.Y, backRect.Width, y2)
            backRect2 = New Rectangle(backRect.X, backRect.Y + y2, backRect.Width, backRect.Height - y2)
            backRect1I = backRect1
            backRect2I = backRect2
            backRect1I.Inflate(1, 1)
            backRect2I.Inflate(1, 1)
            Using fillBrush1 As LinearGradientBrush = New LinearGradientBrush(backRect1I, colors.FillTop1, colors.FillTop2, 90.0F), _
                fillBrush2 = New LinearGradientBrush(backRect2I, colors.FillBottom1, colors.FillBottom2, 90.0F)
                backRect.Inflate(-1, -1)
                y2 = backRect.Height / 2
                backRect1 = New Rectangle(backRect.X, backRect.Y, backRect.Width, y2)
                backRect2 = New Rectangle(backRect.X, backRect.Y + y2, backRect.Width, backRect.Height - y2)
                g.FillRectangle(fillBrush1, backRect1)
                g.FillRectangle(fillBrush2, backRect2)
            End Using
        End Sub
        ''' <summary>
        ''' Draw Gradient Border for MBToolStrip/MBStatusStrip
        ''' </summary>
        Private Sub DrawGradientBorder(ByVal g As Graphics, ByVal backRect As Rectangle, ByVal colors As GradientItemColor)
            Using uaa As UseAntiAlias = New UseAntiAlias(g)
                Dim backRectI As Rectangle = backRect
                backRectI.Inflate(1, 1)
                Using borderBrush As LinearGradientBrush = New LinearGradientBrush(backRectI, colors.Border1, colors.Border2, 90.0F)
                    borderBrush.SetSigmaBellShape(0.5F)
                    Using borderPen As Pen = New Pen(borderBrush)
                        Using borderPath As GraphicsPath = DrawBorderPath(backRect, 1.2)
                            g.DrawPath(borderPen, borderPath)
                        End Using
                    End Using
                End Using
            End Using
        End Sub

#End Region

#End Region

#Region "   Functions"
        ''' <summary>
        ''' Return Color For ColorPicker.
        ''' </summary>
        ''' <param name="red">Red As Int16</param>
        ''' <param name="green">Green As Int16</param>
        ''' <param name="blue">Blue As Int16</param>
        Private Function GetColor(ByVal red As Int16, ByVal green As Int16, ByVal blue As Int16) As Color
            Return Color.FromArgb(red, green, blue)
        End Function
        ''' <summary>
        ''' Return Selected Box For ColorPicker.
        ''' </summary>
        ''' <param name="mouseLocation">MouseLocation As Point</param>
        Private Function GetSelectedBox(ByVal mouseLocation As Point) As Point
            Dim x As Int16 = -1
            Dim y As Int16 = -1
            Dim isPaletteSelected As Boolean = False
            If (mouseLocation.Y >= _TopBoxMargin1 And mouseLocation.Y <= _TopBoxMargin1 + _ColorBoxUnit) Then
                y = 0
            ElseIf (mouseLocation.Y >= _TopBoxMargin2 And mouseLocation.Y <= _TopBoxMargin2 + 5 * _ColorBoxUnit) Then
                y = (mouseLocation.Y - _TopBoxMargin2) / _ColorBoxUnit + 1
            ElseIf (mouseLocation.Y >= _TopBoxMargin3 And mouseLocation.Y <= _TopBoxMargin3 + _ColorBoxUnit) Then
                y = 6
            ElseIf (mouseLocation.Y > _TopBoxMargin3 + _ColorBoxUnit) Then
                isPaletteSelected = True
            Else
                isPaletteSelected = False
            End If

            Dim tmp As Int16 = (mouseLocation.X - _ColorBoxMarginX) Mod (_ColorBoxUnit + _ColorBoxMarginX)
            If (tmp >= 0 And tmp <= _ColorBoxUnit) Then
                x = (mouseLocation.X - _ColorBoxMarginX) / (_ColorBoxUnit + _ColorBoxMarginX)
                If (x >= 10) Then x = -1
            End If
            _IsPaletteSelectedG = isPaletteSelected
            Return New Point(x, y)
        End Function
        ''' <summary>
        ''' Draw Border Path For ColorPicker.
        ''' </summary>
        ''' <param name="rect">Rect As Rectangle</param>
        ''' <param name="exclude">Exclude As Rectangle</param>
        ''' <param name="cut">Cut As Single</param>
        Private Function DrawBorderPath(ByVal rect As Rectangle, ByVal exclude As Rectangle, ByVal cut As Single) As GraphicsPath
            If (exclude.IsEmpty) Then Return DrawBorderPath(rect, cut)
            rect.Width -= 1
            rect.Height -= 1
            Dim pts As List(Of PointF) = New List(Of PointF)
            Dim l As Single = rect.X
            Dim t As Single = rect.Y
            Dim r As Single = rect.Right
            Dim b As Single = rect.Bottom
            Dim x0 As Single = rect.X + cut
            Dim x3 As Single = rect.Right - cut
            Dim y0 As Single = rect.Y + cut
            Dim y3 As Single = rect.Bottom - cut
            Dim cutBack As Single
            If cut = 0.0F Then
                cutBack = 1
            Else
                cutBack = cut
            End If
            If ((rect.Y >= exclude.Top) And (rect.Y <= exclude.Bottom)) Then
                Dim x1 As Single = exclude.X - 1 - cut
                Dim x2 As Single = exclude.Right + cut
                If (x0 <= x1) Then
                    pts.Add(New PointF(x0, t))
                    pts.Add(New PointF(x1, t))
                    pts.Add(New PointF(x1 + cut, t - cutBack))
                Else
                    x1 = exclude.X - 1
                    pts.Add(New PointF(x1, t))
                    pts.Add(New PointF(x1, t - cutBack))
                End If
                If (x3 > x2) Then
                    pts.Add(New PointF(x2 - cut, t - cutBack))
                    pts.Add(New PointF(x2, t))
                    pts.Add(New PointF(x3, t))
                Else
                    x2 = exclude.Right
                    pts.Add(New PointF(x2, t - cutBack))
                    pts.Add(New PointF(x2, t))
                End If
            Else
                pts.Add(New PointF(x0, t))
                pts.Add(New PointF(x3, t))
            End If
            pts.Add(New PointF(r, y0))
            pts.Add(New PointF(r, y3))
            pts.Add(New PointF(x3, b))
            pts.Add(New PointF(x0, b))
            pts.Add(New PointF(l, y3))
            pts.Add(New PointF(l, y0))
            Dim path As GraphicsPath = New GraphicsPath()
            For i As Int32 = 1 To pts.Count - 1
                path.AddLine(pts(i - 1), pts(i))
            Next
            path.AddLine(pts(pts.Count - 1), pts(0))
            Return path
        End Function
        ''' <summary>
        ''' Draw Border Path for MBToolStrip/MBStatusStrip
        ''' </summary>
        Private Function DrawBorderPath(ByVal rect As Rectangle, ByVal cut As Single) As GraphicsPath
            rect.Width -= 1
            rect.Height -= 1
            Dim path As GraphicsPath = New GraphicsPath()
            path.AddLine(rect.Left + cut, rect.Top, rect.Right - cut, rect.Top)
            path.AddLine(rect.Right - cut, rect.Top, rect.Right, rect.Top + cut)
            path.AddLine(rect.Right, rect.Top + cut, rect.Right, rect.Bottom - cut)
            path.AddLine(rect.Right, rect.Bottom - cut, rect.Right - cut, rect.Bottom)
            path.AddLine(rect.Right - cut, rect.Bottom, rect.Left + cut, rect.Bottom)
            path.AddLine(rect.Left + cut, rect.Bottom, rect.Left, rect.Bottom - cut)
            path.AddLine(rect.Left, rect.Bottom - cut, rect.Left, rect.Top + cut)
            path.AddLine(rect.Left, rect.Top + cut, rect.Left + cut, rect.Top)
            Return path
        End Function

#End Region

#Region "Gradient Item Color"
        ''' <summary>
        ''' GradientItemColor Class which Provides Gradient Colors
        ''' </summary>
        Private Class GradientItemColor

#Region "           Public Fields"
            Public InsideTop1 As Color
            Public InsideTop2 As Color
            Public InsideBottom1 As Color
            Public InsideBottom2 As Color
            Public FillTop1 As Color
            Public FillTop2 As Color
            Public FillBottom1 As Color
            Public FillBottom2 As Color
            Public Border1 As Color
            Public Border2 As Color
#End Region

#Region "           Identity"
            ''' <summary>
            ''' Constructor for GradientItemColor Class
            ''' </summary>
            Public Sub New(ByVal _insideTop1 As Color, ByVal _insideTop2 As Color, _
                                          ByVal _insideBottom1 As Color, ByVal _insideBottom2 As Color, _
                                          ByVal _fillTop1 As Color, ByVal _fillTop2 As Color, _
                                          ByVal _fillBottom1 As Color, ByVal _fillBottom2 As Color, _
                                          ByVal _border1 As Color, ByVal _border2 As Color)
                InsideTop1 = _insideTop1
                InsideTop2 = _insideTop2
                InsideBottom1 = _insideBottom1
                InsideBottom2 = _insideBottom2
                FillTop1 = _fillTop1
                FillTop2 = _fillTop2
                FillBottom1 = _fillBottom1
                FillBottom2 = _fillBottom2
                Border1 = _border1
                Border2 = _border2
            End Sub
#End Region

        End Class

#End Region

#Region "   Declaration Of Colors"

        Private _color1 As Color = Color.FromArgb(167, 167, 167)
        Private _color2 As Color = Color.FromArgb(21, 66, 139)
        Private _color3 As Color = Color.FromArgb(76, 83, 92)
        Private _color4 As Color = Color.FromArgb(250, 250, 250)
        Private _color5 As Color = Color.FromArgb(248, 248, 248)
        Private _color6 As Color = Color.FromArgb(243, 243, 243)
        Private _rcolor1 As Color = Color.FromArgb(255, 255, 251)
        Private _rcolor2 As Color = Color.FromArgb(255, 249, 227)
        Private _rcolor3 As Color = Color.FromArgb(255, 242, 201)
        Private _rcolor4 As Color = Color.FromArgb(255, 248, 181)
        Private _rcolor5 As Color = Color.FromArgb(255, 252, 229)
        Private _rcolor6 As Color = Color.FromArgb(255, 235, 166)
        Private _rcolor7 As Color = Color.FromArgb(255, 213, 103)
        Private _rcolor8 As Color = Color.FromArgb(255, 228, 145)
        Private _rcolor9 As Color = Color.FromArgb(160, 188, 228)
        Private _rcolorA As Color = Color.FromArgb(255, 214, 78)
        Private _rcolorB As Color = Color.FromArgb(182, 190, 192)
        Private _rcolorC As Color = Color.FromArgb(155, 163, 167)
        Private _rcolorD As Color = Color.FromArgb(233, 168, 97)
        Private _rcolorE As Color = Color.FromArgb(247, 164, 39)
        Private _rcolorF As Color = Color.FromArgb(246, 156, 24)
        Private _rcolorG As Color = Color.FromArgb(253, 173, 17)
        Private _rcolorH As Color = Color.FromArgb(254, 185, 108)
        Private _rcolorI As Color = Color.FromArgb(253, 164, 97)
        Private _rcolorJ As Color = Color.FromArgb(252, 143, 61)
        Private _rcolorK As Color = Color.FromArgb(255, 208, 134)
        Private _rcolorL As Color = Color.FromArgb(249, 192, 103)
        Private _rcolorM As Color = Color.FromArgb(250, 195, 93)
        Private _rcolorN As Color = Color.FromArgb(248, 190, 81)
        Private _rcolorO As Color = Color.FromArgb(255, 208, 49)
        Private _rcolorP As Color = Color.FromArgb(254, 214, 168)
        Private _rcolorQ As Color = Color.FromArgb(252, 180, 100)
        Private _rcolorR As Color = Color.FromArgb(252, 161, 54)
        Private _rcolorS As Color = Color.FromArgb(254, 238, 170)
        Private _rcolorT As Color = Color.FromArgb(249, 202, 113)
        Private _rcolorU As Color = Color.FromArgb(250, 205, 103)
        Private _rcolorV As Color = Color.FromArgb(248, 200, 91)
        Private _rcolorW As Color = Color.FromArgb(255, 218, 59)
        Private _rcolorX As Color = Color.FromArgb(254, 185, 108)
        Private _rcolorY As Color = Color.FromArgb(252, 161, 54)
        Private _rcolorZ As Color = Color.FromArgb(254, 238, 170)

        Private _textDisabled As Color = _color1
        Private _textMenuStripItem As Color = _color2
        Private _textStatusStripItem As Color = _color2
        Private _textContextMenuItem As Color = _color2
        Private _arrowDisabled As Color = _color1
        Private _arrowLight As Color = Color.FromArgb(106, 126, 197)
        Private _arrowDark As Color = Color.FromArgb(64, 70, 90)
        Private _separatorMenuLight As Color = Color.FromArgb(245, 245, 245)
        Private _separatorMenuDark As Color = Color.FromArgb(197, 197, 197)
        Private _contextMenuBack As Color = _color4
        Private _contextCheckBorder As Color = Color.FromArgb(242, 149, 54)
        Private _contextCheckTick As Color = Color.FromArgb(66, 75, 138)
        Private _statusStripBorderDark As Color = Color.FromArgb(86, 125, 176)
        Private _statusStripBorderLight As Color = Color.White
        Private _gripDark As Color = Color.FromArgb(114, 152, 204)
        Private _gripLight As Color = _color5
        Private _itemContextItemEnabledColors As GradientItemColor = New GradientItemColor(_rcolor1, _rcolor2, _rcolor3, _rcolor4, _rcolor5, _rcolor6, _rcolor7, _rcolor8, Color.FromArgb(217, 203, 150), Color.FromArgb(192, 167, 118))
        Private _itemDisabledColors As GradientItemColor = New GradientItemColor(_color4, _color6, Color.FromArgb(236, 236, 236), Color.FromArgb(230, 230, 230), _color6, Color.FromArgb(224, 224, 224), Color.FromArgb(200, 200, 200), Color.FromArgb(210, 210, 210), Color.FromArgb(212, 212, 212), Color.FromArgb(195, 195, 195))
        Private _itemToolItemSelectedColors As GradientItemColor = New GradientItemColor(_rcolor1, _rcolor2, _rcolor3, _rcolor4, _rcolor5, _rcolor6, _rcolor7, _rcolor8, _rcolor9, _rcolorA)
        Private _itemToolItemPressedColors As GradientItemColor = New GradientItemColor(_rcolorD, _rcolorE, _rcolorF, _rcolorG, _rcolorH, _rcolorI, _rcolorJ, _rcolorK, _rcolor9, _rcolorA)
        Private _itemToolItemCheckedColors As GradientItemColor = New GradientItemColor(_rcolorL, _rcolorM, _rcolorN, _rcolorO, _rcolorP, _rcolorQ, _rcolorR, _rcolorS, _rcolor9, _rcolorA)
        Private _itemToolItemCheckPressColors As GradientItemColor = New GradientItemColor(_rcolorT, _rcolorU, _rcolorV, _rcolorW, _rcolorX, _rcolorI, _rcolorY, _rcolorZ, _rcolor9, _rcolorA)
#End Region

    End Class

#End Region

End Class

#End Region

#Region "UseAntiAlias Class"

Friend Class UseAntiAlias
    Implements IDisposable
    Private disposedValue As Boolean = False        ' To detect redundant calls
    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: free other state (managed objects).
            End If

            ' TODO: free your own state (unmanaged objects).
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

#Region "Instance Fields"
    Private _g As Graphics
    Private _old As SmoothingMode
#End Region

    Public Sub New(ByVal g As Graphics)
        _g = g
        _old = _g.SmoothingMode
        _g.SmoothingMode = SmoothingMode.AntiAlias
    End Sub

    Protected Overrides Sub Finalize()
        _g.SmoothingMode = _old
        MyBase.Finalize()
    End Sub

End Class

#End Region

#Region "UseClearTypeGridFit Class"

Friend Class UseClearTypeGridFit
    Implements IDisposable

    Private disposedValue As Boolean = False        ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: free other state (managed objects).
            End If

            ' TODO: free your own state (unmanaged objects).
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

#Region "Instance Fields"
    Private _g As Graphics
    Private _old As TextRenderingHint
#End Region

    Public Sub New(ByVal g As Graphics)
        _g = g
        _old = _g.TextRenderingHint
        _g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit
    End Sub

    Protected Overrides Sub Finalize()
        _g.TextRenderingHint = _old
        MyBase.Finalize()
    End Sub

End Class

#End Region

#Region "Use Clipping Class"

Friend Class UseClipping
    Implements IDisposable

    Private disposedValue As Boolean = False        ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: free other state (managed objects).
            End If

            ' TODO: free your own state (unmanaged objects).
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

#Region "Instance Fields"
    Private _g As Graphics
    Private _old As Region
#End Region
    ''' <summary>
    ''' Constructor for UsingClipping Class
    ''' </summary>
    Public Sub New(ByVal g As Graphics, ByVal path As GraphicsPath)
        _g = g
        _old = g.Clip
        Dim clip As Region = _old.Clone()
        clip.Intersect(path)
        _g.Clip = clip
    End Sub

    Public Sub New(ByVal g As Graphics, ByVal region As Region)
        _g = g
        _old = g.Clip
        Dim clip As Region = _old.Clone()
        clip.Intersect(region)
        _g.Clip = clip
    End Sub

    Protected Overrides Sub Finalize()
        _g.Clip = _old
        MyBase.Finalize()
    End Sub

End Class

#End Region